#include "utils.h"
#include "linebuf.h"

struct linebuf {
	char buf[LINEBUF_MAX];
  int buf_size;
};

static struct linebuf lb = {.buf={0}, .buf_size=0,};

void init_linebuf(void)
{
  lb.buf[0] = 0;
  lb.buf_size = 0;
}

int add_linebuf(const char *s, int len)
{
	if (lb.buf_size + len < LINEBUF_MAX) {
    memcpy(lb.buf + lb.buf_size, s, len);
    lb.buf_size += len;
    lb.buf[lb.buf_size] = 0;
		return len;
	} else 
		return 0;
}

int backspace_linebuf(void)
{
	if (lb.buf_size > 0) {
    --lb.buf_size;
		lb.buf[lb.buf_size] = 0;
		return 1;
	} else 
		return 0;
}

int get_linebuf(const char **buf)
{
  if (buf != NULL)
    *buf = lb.buf;
  return lb.buf_size;
}

